package com.journaldev.spring.util;

import java.beans.PropertyEditorSupport;

import com.journaldev.spring.model.District;
import com.journaldev.spring.service.DistrictService;

public class SimpleDistrictEditor extends PropertyEditorSupport {
	
	private DistrictService districtDao;
	
	public SimpleDistrictEditor(DistrictService districtDao){
        this.districtDao = districtDao;
    }
	
	@Override
    public void setAsText(String text) throws IllegalArgumentException {
    	District district = null;
        try {
            Integer id = Integer.parseInt(text);
            district = districtDao.getDistrictById(id);
            System.out.println("District name:" + district.getDistrictName());
        } catch (NumberFormatException ex) {
            System.out.println("District will be null");
        }
        setValue(district);
    }

}
